package SwingView.Assignment;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAssignmentSubmission;

public class LastSubmissionPanel extends JPanel{
	
	private JPanel jpFiles;
	private JLabel lblSubmittedTime;
	private List<FileButton> listOfFilePanel;
	private JLabel lblTitle;
	private JLabel lblGrade;

	public LastSubmissionPanel(CmsAssignmentSubmission asgmSub) {
		setLayout(new GridBagLayout());
		setBackground(Color.WHITE);
		setForeground(new Color(147,219,147));
		setOpaque(true);
		setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		listOfFilePanel = new ArrayList();
		
		//Files Label
		lblTitle = new JLabel("You have turned in your submission:");
		GridBagConstraints gbc_lblFiles = new GridBagConstraints();
		gbc_lblFiles.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblFiles.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblFiles.insets = new Insets(10, 10, 10, 5);
		gbc_lblFiles.weightx = 0.01;
		gbc_lblFiles.gridx = 0;
		gbc_lblFiles.gridy = 0;
		add(lblTitle, gbc_lblFiles);
		lblTitle.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		
		lblSubmittedTime = new JLabel(CmsPresenter.sdf.format(new Date(asgmSub.getCreatedDate().getTime())));
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(10, 10, 10, 10);
		c.weightx = 1.0;
		c.gridy = 0;
		c.gridx = 1;
		add(lblSubmittedTime,c);
		
		JLabel jlGrade = new JLabel("You got " + asgmSub.getGivenPoint() + " out of 100.0 points.");
		jlGrade.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0, 10, 10, 5);
		c.gridx = 0;
		c.gridy = 1;
		add(jlGrade, c);
		
		
		jpFiles = new JPanel(new GridBagLayout());
		jpFiles.setOpaque(false);
		GridBagConstraints gbc_jpFiles = new GridBagConstraints();
		gbc_jpFiles.anchor = GridBagConstraints.LINE_START;
		gbc_jpFiles.insets = new Insets(10, 40, 10, 40);
		gbc_jpFiles.gridwidth = 2;
		gbc_jpFiles.weighty = 1.0;
		gbc_jpFiles.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpFiles.gridx = 0;
		gbc_jpFiles.gridy = 2;
		add(jpFiles, gbc_jpFiles);
		
		//add files to files panel
		int i = 0;
		if(asgmSub.getSubFiles().size() <= 0){
			lblTitle.setText("You have no submission!");
			setForeground(new Color(255,195,182)); // set red color
			jpFiles.setVisible(false);
			lblSubmittedTime.setVisible(false);
		}
		else {
			for(File file : asgmSub.getSubFiles()){
				c = new GridBagConstraints();
				c.gridy = i;
				c.anchor = GridBagConstraints.FIRST_LINE_START;
				c.insets = new Insets(0,0,10,0);
				jpFiles.add(new FileButton(file),c);
				i++;
			}
			
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = listOfFilePanel.size();
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpFiles.add(p,c_p);
		
		jpFiles.repaint();
		jpFiles.revalidate();
	}
	
	
	public void setTitle(String title){
		lblTitle.setText(title);
	}
	
	class FileButton extends JButton{
		
		public File attachedFile;
		
		public FileButton(File file){
			super("Submission File: " + file.getName());
			this.attachedFile = file;
			setForeground(Color.BLUE);
			setOpaque(false);
			setBorder(BorderFactory.createEmptyBorder());
			setCursor(new Cursor(Cursor.HAND_CURSOR));
			addActionListener(new ALDownloadFile(file));
		}
	}

	
	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		g.fillRoundRect(10, 10, this.getWidth()-20, this.getHeight()-20, 10, 10);
	}	
}
